package com.canbot.u05.activity.presidemode.newcode.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.bean.ActionBean;
import com.canbot.u05.activity.presidemode.newcode.bean.ModuleUtil;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IItemDragClick;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NChoiceActionAdapter extends RecyclerView.Adapter<NChoiceActionAdapter.ActionExistViewHolder> {
    private boolean isShowNumber = false;
    private List<ActionBean> actionBeanList;
    private LayoutInflater layoutInflater;
    public boolean isHeadActionShow = true;
    private IItemDragClick iItemClick;

    public void setiItemClick(IItemDragClick iItemClick) {
        this.iItemClick = iItemClick;
    }

    public NChoiceActionAdapter(Context context, boolean isShowNumber, List<ActionBean> actionBeanList) {
        this.isShowNumber = isShowNumber;
        this.actionBeanList = actionBeanList;
        if (this.actionBeanList == null) {
            this.actionBeanList = new ArrayList<>();
        }
        layoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public ActionExistViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = layoutInflater.inflate(R.layout.item_choice_action, null, false);
        return new ActionExistViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ActionExistViewHolder actionExistViewHolder, int i) {
        if (isShowNumber) {
            actionExistViewHolder.actionIndex.setVisibility(View.VISIBLE);
            actionExistViewHolder.actionIndex.setText(String.valueOf(i + 1));
        }
        ActionBean actionBean = actionBeanList.get(i);
        actionExistViewHolder.actionName.setText(actionBean.actionName);
        actionExistViewHolder.actionTime.setText(ModuleUtil.translateTime(actionBean.time));
        actionExistViewHolder.itemActionMain.setOnClickListener(v -> {
            if (iItemClick != null) {
                iItemClick.onClick(i, v);
            }
        });
    }

    @Override
    public int getItemCount() {
        return actionBeanList.size();
    }

    public void update(List<ActionBean> armActionList, boolean isHeadActionShow) {
        this.actionBeanList = armActionList;
        this.isHeadActionShow = isHeadActionShow;
        notifyDataSetChanged();
    }

    public class ActionExistViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.actionIndex)
        TextView actionIndex;
        @BindView(R.id.actionName)
        TextView actionName;
        @BindView(R.id.actionTime)
        TextView actionTime;
        @BindView(R.id.itemActionMain)
        RelativeLayout itemActionMain;

        public ActionExistViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            if (isShowNumber) {
                itemView.setOnTouchListener((v, event) -> {
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        if (iItemClick != null) {
                            iItemClick.onStartDrag(NChoiceActionAdapter.ActionExistViewHolder.this);
                        }
                    }
                    return false;
                });
            }
        }
    }
}
